#include "client_h.h"

int main(int argc, char const *argv[])
{
    if(argc!=3){
        printf("Usage:%s<ip><port>\n",argv[0]);
        exit(-1);
    }

    int n;
    MSG msg;
    int sockfd=-1;
    struct sockaddr_in servaddr;
    socklen_t addrlen=sizeof(servaddr);

    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
        ERRLOG("socket error");
    }

    memset(&servaddr,0,addrlen);
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=inet_addr(argv[1]);
    servaddr.sin_port=htons(atoi(argv[2]));

    if(connect(sockfd,(struct sockaddr*)&servaddr,addrlen)==-1){
        ERRLOG("connect error");
    }

    while(1){
        play_client();
        if(scanf("%d",&n)!=1){
            printf("输入格式错误\n");
            while(getchar()!='\n');
            continue;
        }
        getchar();

        switch(n){
            case 1:
                        do_register(sockfd,&msg);
                        break;
            case 2:
                        if(do_login(sockfd,&msg)==1){
                            do_next_login(sockfd,&msg);
                        }
                        break;
            case 3:
                        close(sockfd);
                        exit(0);
                        break;
            default:
                        printf("非法命令\n");
        }
    }
    return 0;
}


